
#__slots__

#限制绑定实例的属性

class Student(object):
    __slots__ = ('name','age')


student1=Student()

student1.name='huangxl'
student1.age=24

print(student1.name)
print(student1.age)

student1.sex='男'
print(student1.sex)

#限制set属性
class Student1(object):

    def get_score(self):
         return self._score

    def set_score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value


#@property
class Student(object):

    @property #get方式
    def score(self):
        return self._score

    @score.setter #set方式
    def score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value

# 注意到这个神奇的@property，我们在对实例属性操作的时候，
# 就知道该属性很可能不是直接暴露的，而是通过getter和setter方法来实现的。